<?php

class SubjectController extends Controller {

    public  $layout = 'application.modules.admin.views.layouts.main';
    public $model;
    
    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array(''),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform any actions
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }
    
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete,update', // we only allow deletion via POST request
        );
    }
    
    public function actionIndex() {
        $criteria = new CDbCriteria();
        if(isset($_GET['id']) && $_GET['id'] < 1000000){
            $criteria->condition = 'status=' . $_GET['id'];
        }else{
            $criteria->condition = 'status<'.  Subject::STATUS_INVALID;
        }
        $criteria->order = 'create_time DESC';
        $dataProvider = new CActiveDataProvider('Subject', array(
                    'pagination' => array(
                        'pageSize' => 5,
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionCreate() {
        if(isset($_POST['subject_id']) && !empty($_POST['subject_id'])){
            $this->model = $this->loadModel($_POST['subject_id']);
        }else{
            $this->model = new Subject();
        }
        $subject = $this->model;
        if(isset($_POST['Subject'])){
            $subject->attributes = $_POST['Subject'];
            if ($subject->validate()) {
                if($subject->isNewRecord)
                    $subject->save();
                else
                    $subject->update();
                $this->model = $subject;
                $this->redirect('index');
            }
        }
        
        $this->render('create',array('subject' => $subject));
    }

    public function loadModel($id) {
        if ($this->model === NULL) {
            $this->model = Subject::model()->findByPk($id);
            if ($this->model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->model;
    }
    
    public function actionUpdate(){
        if (isset($_POST['subject_id'])) {
            $model = $this->loadModel($_POST['subject_id']);
            if (isset($model)) {
                $this->render('create', array(
                    'subject' => $model,
                ));
                Yii::app()->end();
            }
        }
        $this->redirect('index');
    }
    
    public function actionDelete(){
        if (Yii::app()->request->isPostRequest) {
            if (isset($_POST['subject_id']) && !empty($_POST['subject_id'])) {
                Subject::model()->deleteSubjectById($_POST['subject_id']);
            }
            $this->redirect('index');
        }else
            throw new CHttpException(404, 'Invalid request. Please do not repeat this request again.');
    }
    
    public function actionPass(){
        if (Yii::app()->request->isPostRequest) {
            if (isset($_POST['subject_id']) && !empty($_POST['subject_id'])) {
                Subject::model()->updateByPk($_POST['subject_id'], array('status' => Subject::STATUS_OK));
            }
            $this->redirect('index');
        }else
            throw new CHttpException(404, 'Invalid request. Please do not repeat this request again.');
    }
}